<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>智能家居控制中心 </title>
    <script src="https://unpkg.com/mqtt/dist/mqtt.min.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&family=Roboto+Mono:wght@500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
    
    <style>
        :root {
            --primary: #6366F1;
            --primary-light: #818CF8;
            --primary-dark: #4F46E5;
            --secondary: #10B981;
            --accent: #F59E0B;
            --bg: #0F172A;
            --card-bg: rgba(30, 41, 59, 0.7);
            --card-hover: rgba(51, 65, 85, 0.8);
            --text-primary: #F1F5F9;
            --text-secondary: #94A3B8;
            --success: #10B981;
            --warning: #F59E0B;
            --danger: #EF4444;
            --border-radius: 16px;
            --transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
            --shadow: 0 4px 20px rgba(0, 0, 0, 0.2);
            --shadow-hover: 0 8px 30px rgba(99, 102, 241, 0.3);
            --glass: rgba(255, 255, 255, 0.05);
            --glass-border: rgba(255, 255, 255, 0.1);
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Inter', sans-serif;
            background: linear-gradient(135deg, #0F172A 0%, #1E293B 100%);
            color: var(--text-primary);
            min-height: 100vh;
            line-height: 1.6;
            overflow-x: hidden;
        }

        /* 动态背景 */
        .dynamic-bg {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            z-index: -1;
            background: 
                radial-gradient(circle at 15% 50%, rgba(99, 102, 241, 0.15) 0%, transparent 25%),
                radial-gradient(circle at 85% 30%, rgba(16, 185, 129, 0.1) 0%, transparent 25%),
                radial-gradient(circle at 50% 80%, rgba(245, 158, 11, 0.1) 0%, transparent 25%);
            background-attachment: fixed;
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
            position: relative;
            z-index: 1;
        }

        header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 40px;
            padding: 20px 0;
            border-bottom: 1px solid var(--glass-border);
        }

        .logo {
            display: flex;
            align-items: center;
            gap: 16px;
        }

        .logo-icon {
            width: 50px;
            height: 50px;
            background: linear-gradient(135deg, var(--primary), var(--primary-light));
            border-radius: 14px;
            display: flex;
            align-items: center;
            justify-content: center;
            box-shadow: 0 6px 20px rgba(99, 102, 241, 0.4);
            position: relative;
            overflow: hidden;
        }

        .logo-icon::after {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: linear-gradient(135deg, rgba(255,255,255,0.2), transparent);
        }

        .logo-icon i {
            font-size: 1.6rem;
            color: white;
            z-index: 1;
        }

        .logo-text {
            font-size: 1.8rem;
            font-weight: 700;
            background: linear-gradient(135deg, var(--text-primary), var(--text-secondary));
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
        }

        .system-info {
            display: flex;
            align-items: center;
            gap: 20px;
        }

        .connection-status {
            display: flex;
            align-items: center;
            gap: 10px;
            font-size: 0.95rem;
            padding: 8px 18px;
            background-color: var(--glass);
            border-radius: 30px;
            border: 1px solid var(--glass-border);
            backdrop-filter: blur(10px);
        }

        .status-dot {
            width: 10px;
            height: 10px;
            border-radius: 50%;
        }

        .status-online {
            background-color: var(--success);
            box-shadow: 0 0 10px rgba(16, 185, 129, 0.7);
            animation: pulse 2s infinite;
        }

        .status-offline {
            background-color: var(--warning);
            box-shadow: 0 0 10px rgba(245, 158, 11, 0.7);
        }

        @keyframes pulse {
            0% { transform: scale(1); opacity: 1; }
            50% { transform: scale(1.1); opacity: 0.7; }
            100% { transform: scale(1); opacity: 1; }
        }

        .device-id {
            font-size: 0.95rem;
            color: var(--text-secondary);
            padding: 8px 18px;
            background-color: var(--glass);
            border-radius: 30px;
            border: 1px solid var(--glass-border);
            backdrop-filter: blur(10px);
        }

        .dashboard-title {
            font-size: 1.4rem;
            color: var(--text-primary);
            margin-bottom: 24px;
            font-weight: 600;
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .dashboard-title i {
            color: var(--primary-light);
        }

        .sensors-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
            gap: 24px;
            margin-bottom: 30px;
        }

        .sensor-card {
            background-color: var(--card-bg);
            border-radius: var(--border-radius);
            padding: 24px;
            box-shadow: var(--shadow);
            transition: var(--transition);
            position: relative;
            overflow: hidden;
            border: 1px solid var(--glass-border);
            backdrop-filter: blur(10px);
        }

        .sensor-card:hover {
            transform: translateY(-5px);
            box-shadow: var(--shadow-hover);
            background-color: var(--card-hover);
        }

        .sensor-card::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 4px;
            background: linear-gradient(90deg, var(--primary), var(--secondary));
        }

        .sensor-header {
            display: flex;
            justify-content: space-between;
            align-items: flex-start;
            margin-bottom: 20px;
        }

        .sensor-title-group {
            display: flex;
            align-items: center;
            gap: 12px;
        }

        .sensor-icon {
            width: 50px;
            height: 50px;
            border-radius: 12px;
            display: flex;
            align-items: center;
            justify-content: center;
            background: linear-gradient(135deg, var(--primary), var(--primary-light));
            box-shadow: 0 4px 10px rgba(99, 102, 241, 0.3);
        }

        .sensor-icon i {
            color: white;
            font-size: 1.3rem;
        }

        .sensor-title {
            font-size: 1.2rem;
            font-weight: 600;
        }

        .sensor-update-time {
            font-size: 0.85rem;
            color: var(--text-secondary);
            text-align: right;
        }

        .sensor-data {
            margin: 16px 0;
        }

        .sensor-value {
            font-family: 'Roboto Mono', monospace;
            font-size: 2.8rem;
            font-weight: 700;
            color: var(--text-primary);
            transition: var(--transition);
            line-height: 1.2;
            display: flex;
            align-items: baseline;
        }

        .sensor-value.high {
            color: var(--danger);
        }

        .sensor-value.low {
            color: var(--warning);
        }

        .sensor-unit {
            font-size: 1.2rem;
            color: var(--text-secondary);
            margin-left: 8px;
            font-weight: 500;
        }

        .sensor-status {
            display: flex;
            align-items: center;
            gap: 8px;
            font-size: 0.9rem;
            margin-top: 12px;
        }

        .status-normal {
            color: var(--success);
        }

        .status-warning {
            color: var(--warning);
        }

        .status-error {
            color: var(--danger);
        }

        .chart-container {
            height: 90px;
            margin-top: 20px;
        }

        .controls-section {
            background-color: var(--card-bg);
            border-radius: var(--border-radius);
            padding: 24px;
            box-shadow: var(--shadow);
            border: 1px solid var(--glass-border);
            backdrop-filter: blur(10px);
        }

        .section-title {
            font-size: 1.3rem;
            font-weight: 600;
            margin-bottom: 20px;
            display: flex;
            align-items: center;
            gap: 10px;
            color: var(--text-primary);
        }

        .section-title i {
            font-size: 1.2rem;
            color: var(--primary-light);
        }

        .controls-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
            gap: 20px;
        }

        .control-card {
            background-color: rgba(255, 255, 255, 0.05);
            border-radius: 12px;
            padding: 20px;
            transition: var(--transition);
            border: 1px solid var(--glass-border);
        }

        .control-card:hover {
            background-color: rgba(255, 255, 255, 0.08);
            transform: translateY(-3px);
        }

        .control-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 16px;
        }

        .control-name {
            font-size: 1.1rem;
            font-weight: 500;
            display: flex;
            align-items: center;
            gap: 8px;
        }

        .control-name i {
            color: var(--primary-light);
            font-size: 1.1rem;
        }

        .control-state {
            font-size: 0.85rem;
            padding: 5px 12px;
            border-radius: 20px;
            background-color: rgba(99, 102, 241, 0.15);
            color: var(--primary-light);
            font-weight: 500;
        }

        .control-buttons {
            display: flex;
            gap: 12px;
        }

        .control-btn {
            flex: 1;
            padding: 12px 16px;
            border: none;
            border-radius: 10px;
            font-size: 0.95rem;
            font-weight: 600;
            cursor: pointer;
            transition: var(--transition);
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 8px;
        }

        .btn-on {
            background: linear-gradient(135deg, var(--primary), var(--primary-light));
            color: white;
            box-shadow: 0 4px 10px rgba(99, 102, 241, 0.3);
        }

        .btn-on:hover {
            background: linear-gradient(135deg, var(--primary-light), var(--primary));
            transform: translateY(-2px);
            box-shadow: 0 6px 15px rgba(99, 102, 241, 0.4);
        }

        .btn-off {
            background-color: rgba(255, 255, 255, 0.1);
            color: var(--text-secondary);
            border: 1px solid var(--glass-border);
        }

        .btn-off:hover {
            background-color: rgba(255, 255, 255, 0.15);
            color: var(--text-primary);
            transform: translateY(-2px);
        }

        .btn-alert {
            background: linear-gradient(135deg, var(--danger), #F87171);
            color: white;
            box-shadow: 0 4px 10px rgba(239, 68, 68, 0.3);
        }

        .btn-alert:hover {
            background: linear-gradient(135deg, #F87171, var(--danger));
            transform: translateY(-2px);
            box-shadow: 0 6px 15px rgba(239, 68, 68, 0.4);
        }

        footer {
            text-align: center;
            margin-top: 60px;
            padding: 24px 0;
            color: var(--text-secondary);
            font-size: 0.9rem;
            border-top: 1px solid var(--glass-border);
        }

        .loading {
            position: relative;
            display: inline-block;
        }

        .loading::after {
            content: '';
            position: absolute;
            width: 100%;
            height: 2px;
            bottom: -2px;
            left: 0;
            background: linear-gradient(90deg, transparent, var(--primary), transparent);
            animation: loading 1.5s ease-in-out infinite;
        }

        @keyframes loading {
            0% { background-position: -100% 0; }
            100% { background-position: 100% 0; }
        }

        @media (max-width: 768px) {
            header {
                flex-direction: column;
                align-items: flex-start;
                gap: 16px;
            }

            .system-info {
                width: 100%;
                justify-content: space-between;
            }

            .sensors-grid {
                grid-template-columns: 1fr;
            }

            .sensor-value {
                font-size: 2.2rem;
            }

            .control-buttons {
                flex-direction: column;
            }
        }
    </style>
</head>
<body>
    <!-- 动态背景 -->
    <div class="dynamic-bg"></div>

    <div class="container">
        <header>
            <div class="logo">
                <div class="logo-icon">
                    <i class="fas fa-home-automation"></i>
                </div>
                <div class="logo-text">智能家居控制中心</div>
            </div>
            
            <div class="system-info">
                <div class="connection-status">
                    <span class="status-dot status-online" id="connectionStatus"></span>
                    <span id="connectionText">已连接</span>
                </div>
                <div class="device-id" id="deviceId">设备 ID: MOYU</div>
            </div>
        </header>

        <h1 class="dashboard-title">
            <i class="fas fa-chart-line"></i>环境监测
        </h1>
        
        <div class="sensors-grid">
            <!-- 温度传感器 -->
            <div class="sensor-card">
                <div class="sensor-header">
                    <div class="sensor-title-group">
                        <div class="sensor-icon">
                            <i class="fas fa-thermometer-half"></i>
                        </div>
                        <div class="sensor-title">温度监测</div>
                    </div>
                    <div class="sensor-update-time" id="tempTime">更新于: 加载中...</div>
                </div>
                <div class="sensor-data">
                    <div class="sensor-value loading" id="temperature">--<span class="sensor-unit">°C</span></div>
                    <div class="sensor-status">
                        <i class="fas fa-circle" style="font-size: 0.6rem; color: var(--success);"></i>
                        <span class="status-normal" id="tempStatus">温度正常</span>
                    </div>
                </div>
                <div class="chart-container">
                    <canvas id="tempChart"></canvas>
                </div>
            </div>

            <!-- 湿度传感器 -->
            <div class="sensor-card">
                <div class="sensor-header">
                    <div class="sensor-title-group">
                        <div class="sensor-icon">
                            <i class="fas fa-tint"></i>
                        </div>
                        <div class="sensor-title">湿度监测</div>
                    </div>
                    <div class="sensor-update-time" id="humidTime">更新于: 加载中...</div>
                </div>
                <div class="sensor-data">
                    <div class="sensor-value loading" id="humidity">--<span class="sensor-unit">%RH</span></div>
                    <div class="sensor-status">
                        <i class="fas fa-circle" style="font-size: 0.6rem; color: var(--success);"></i>
                        <span class="status-normal" id="humidStatus">湿度正常</span>
                    </div>
                </div>
                <div class="chart-container">
                    <canvas id="humidChart"></canvas>
                </div>
            </div>

            <!-- 光照传感器 -->
            <div class="sensor-card">
                <div class="sensor-header">
                    <div class="sensor-title-group">
                        <div class="sensor-icon">
                            <i class="fas fa-sun"></i>
                        </div>
                        <div class="sensor-title">光照监测</div>
                    </div>
                    <div class="sensor-update-time" id="lightTime">更新于: 加载中...</div>
                </div>
                <div class="sensor-data">
                    <div class="sensor-value loading" id="light">--<span class="sensor-unit">Lux</span></div>
                    <div class="sensor-status">
                        <i class="fas fa-circle" style="font-size: 0.6rem; color: var(--success);"></i>
                        <span class="status-normal" id="lightStatus">光照正常</span>
                    </div>
                </div>
                <div class="chart-container">
                    <canvas id="lightChart"></canvas>
                </div>
            </div>
        </div>

        <div class="controls-section">
            <h2 class="section-title">
                <i class="fas fa-sliders-h"></i>设备控制
            </h2>
            <div class="controls-grid">
                <!-- 灯光控制 -->
                <div class="control-card">
                    <div class="control-header">
                        <div class="control-name">
                            <i class="fas fa-lightbulb"></i>智能灯光
                        </div>
                        <div class="control-state" id="ledState">关闭</div>
                    </div>
                    <div class="control-buttons">
                        <button class="control-btn btn-on" id="ledOn">
                            <i class="fas fa-power-off"></i> 开启
                        </button>
                        <button class="control-btn btn-off" id="ledOff">
                            <i class="fas fa-power-off"></i> 关闭
                        </button>
                    </div>
                </div>

                <!-- 报警控制 -->
                <div class="control-card">
                    <div class="control-header">
                        <div class="control-name">
                            <i class="fas fa-bell"></i>报警系统
                        </div>
                        <div class="control-state" id="beepState">静音</div>
                    </div>
                    <div class="control-buttons">
                        <button class="control-btn btn-alert" id="beepOn">
                            <i class="fas fa-volume-up"></i> 报警
                        </button>
                        <button class="control-btn btn-off" id="beepOff">
                            <i class="fas fa-volume-mute"></i> 静音
                        </button>
                    </div>
                </div>
            </div>
        </div>

        <footer>
            <p>智能家居控制系统 &copy; 2025 | By:MOYU</p>
        </footer>
    </div>

    <script>
        // 图表配置
        const chartConfig = {
            responsive: true,
            maintainAspectRatio: false,
            plugins: {
                legend: { display: false }
            },
            scales: {
                x: { display: false },
                y: { display: false }
            },
            elements: {
                line: {
                    tension: 0.4,
                    borderWidth: 3,
                    fill: true
                },
                point: { radius: 0 }
            },
            animation: {
                duration: 1000,
                easing: 'easeOutQuart'
            }
        };

        // 初始化图表
        const tempChart = new Chart(document.getElementById('tempChart'), {
            type: 'line',
            data: {
                labels: Array(10).fill(''),
                datasets: [{
                    data: Array(10).fill(25),
                    borderColor: '#6366F1',
                    backgroundColor: 'rgba(99, 102, 241, 0.1)',
                }]
            },
            options: chartConfig
        });

        const humidChart = new Chart(document.getElementById('humidChart'), {
            type: 'line',
            data: {
                labels: Array(10).fill(''),
                datasets: [{
                    data: Array(10).fill(50),
                    borderColor: '#10B981',
                    backgroundColor: 'rgba(16, 185, 129, 0.1)',
                }]
            },
            options: chartConfig
        });

        const lightChart = new Chart(document.getElementById('lightChart'), {
            type: 'line',
            data: {
                labels: Array(10).fill(''),
                datasets: [{
                    data: Array(10).fill(500),
                    borderColor: '#F59E0B',
                    backgroundColor: 'rgba(245, 158, 11, 0.1)',
                }]
            },
            options: chartConfig
        });

        // 更新图表数据
        function updateChart(chart, value) {
            chart.data.datasets[0].data.shift();
            chart.data.datasets[0].data.push(parseFloat(value));
            chart.update('active');
        }

        // 获取URL参数
        function getQueryParam(param) {
            const url = window.location.href;
            const regex = new RegExp('[?&]' + param + '=([^&#]*)');
            const results = regex.exec(url);
            return results ? decodeURIComponent(results[1]) || '' : null;
        }

        // MQTT配置
        const deviceId = getQueryParam('did') || 'MOYU';
        const clientId = 'mqttjs_' + Math.random().toString(16).substr(2, 8);
        const host = 'ws://60.205.186.62:8083/mqtt';
        const options = {
            keepalive: 60,
            clientId: clientId,
            protocolId: 'MQTT',
            protocolVersion: 4,
            clean: true,
            reconnectPeriod: 1000,
            connectTimeout: 30 * 1000,
            will: {
                topic: 'WillMsg',
                payload: 'Connection Closed abnormally..!',
                qos: 0,
                retain: false
            }
        };

        // 连接MQTT服务器
        const client = mqtt.connect(host, options);

        // 连接成功
        client.on('connect', function() {
            console.log('MQTT连接成功');
            document.getElementById('connectionStatus').className = 'status-dot status-online';
            document.getElementById('connectionText').textContent = '已连接';
            document.getElementById('deviceId').textContent = `设备 ID: ${deviceId}`;

            // 订阅主题
            client.subscribe(deviceId + '/sensor/+', function(err) {
                if (!err) console.log('订阅传感器主题成功');
            });
            client.subscribe(deviceId + '/state/+', function(err) {
                if (!err) console.log('订阅设备状态主题成功');
            });
        });

        // 重连中
        client.on('reconnect', function() {
            document.getElementById('connectionStatus').className = 'status-dot status-offline';
            document.getElementById('connectionText').textContent = '连接中';
        });

        // 连接错误
        client.on('error', function(err) {
            console.log('MQTT连接错误:', err);
            document.getElementById('connectionStatus').className = 'status-dot status-offline';
            document.getElementById('connectionText').textContent = '连接中';
        });

        // 断开连接
        client.on('offline', function() {
            document.getElementById('connectionStatus').className = 'status-dot status-offline';
            document.getElementById('connectionText').textContent = '连接中';
        });

        // 获取当前时间
        function getCurrentTime() {
            const now = new Date();
            return now.toLocaleTimeString('zh-CN', { hour12: false });
        }

        // 处理MQTT消息
        client.on('message', function(topic, message) {
            const data = message.toString();
            const time = getCurrentTime();

            // 温度湿度数据
            if (topic === deviceId + '/sensor/dht11') {
                const [temp, humid] = data.split('_');
                if (temp && humid) {
                    // 更新温度
                    const tempElem = document.getElementById('temperature');
                    tempElem.textContent = temp;
                    tempElem.classList.remove('loading');
                    document.getElementById('tempTime').textContent = `更新于: ${time}`;
                    updateChart(tempChart, temp);

                    // 温度状态判断
                    const tempVal = parseFloat(temp);
                    const tempStatus = document.getElementById('tempStatus');
                    const tempDot = tempStatus.previousElementSibling;
                    
                    if (tempVal > 30) {
                        tempElem.classList.add('high');
                        tempStatus.className = 'status-error';
                        tempStatus.textContent = '温度过高';
                        tempDot.style.color = 'var(--danger)';
                    } else if (tempVal < 10) {
                        tempElem.classList.add('low');
                        tempStatus.className = 'status-warning';
                        tempStatus.textContent = '温度过低';
                        tempDot.style.color = 'var(--warning)';
                    } else {
                        tempElem.classList.remove('high', 'low');
                        tempStatus.className = 'status-normal';
                        tempStatus.textContent = '温度正常';
                        tempDot.style.color = 'var(--success)';
                    }

                    // 更新湿度
                    const humidElem = document.getElementById('humidity');
                    humidElem.textContent = humid;
                    humidElem.classList.remove('loading');
                    document.getElementById('humidTime').textContent = `更新于: ${time}`;
                    updateChart(humidChart, humid);

                    // 湿度状态判断
                    const humidVal = parseFloat(humid);
                    const humidStatus = document.getElementById('humidStatus');
                    const humidDot = humidStatus.previousElementSibling;
                    
                    if (humidVal > 70) {
                        humidElem.classList.add('high');
                        humidStatus.className = 'status-warning';
                        humidStatus.textContent = '湿度过高';
                        humidDot.style.color = 'var(--warning)';
                    } else if (humidVal < 20) {
                        humidElem.classList.add('low');
                        humidStatus.className = 'status-warning';
                        humidStatus.textContent = '湿度过低';
                        humidDot.style.color = 'var(--warning)';
                    } else {
                        humidElem.classList.remove('high', 'low');
                        humidStatus.className = 'status-normal';
                        humidStatus.textContent = '湿度正常';
                        humidDot.style.color = 'var(--success)';
                    }
                }
            }

            // 光照数据
            else if (topic === deviceId + '/sensor/light') {
                const lightElem = document.getElementById('light');
                lightElem.textContent = data;
                lightElem.classList.remove('loading');
                document.getElementById('lightTime').textContent = `更新于: ${time}`;
                updateChart(lightChart, data);

                // 光照状态判断
                const lightVal = parseFloat(data);
                const lightStatus = document.getElementById('lightStatus');
                const lightDot = lightStatus.previousElementSibling;
                
                if (lightVal > 3000) {
                    lightElem.classList.add('high');
                    lightStatus.className = 'status-warning';
                    lightStatus.textContent = '光照过强';
                    lightDot.style.color = 'var(--warning)';
                } else if (lightVal < 500) {
                    lightElem.classList.add('low');
                    lightStatus.className = 'status-warning';
                    lightStatus.textContent = '光照不足';
                    lightDot.style.color = 'var(--warning)';
                } else {
                    lightElem.classList.remove('high', 'low');
                    lightStatus.className = 'status-normal';
                    lightStatus.textContent = '光照正常';
                    lightDot.style.color = 'var(--success)';
                }
            }

            // LED状态
            else if (topic === deviceId + '/state/led') {
                const ledState = document.getElementById('ledState');
                const ledOnBtn = document.getElementById('ledOn');
                const ledOffBtn = document.getElementById('ledOff');
                
                if (data === '1') {
                    ledState.textContent = '开启';
                    ledState.style.backgroundColor = 'rgba(16, 185, 129, 0.15)';
                    ledState.style.color = 'var(--success)';
                    ledOnBtn.classList.add('btn-on');
                    ledOnBtn.classList.remove('btn-off');
                    ledOffBtn.classList.add('btn-off');
                    ledOffBtn.classList.remove('btn-on');
                } else {
                    ledState.textContent = '关闭';
                    ledState.style.backgroundColor = 'rgba(99, 102, 241, 0.15)';
                    ledState.style.color = 'var(--primary-light)';
                    ledOffBtn.classList.add('btn-on');
                    ledOffBtn.classList.remove('btn-off');
                    ledOnBtn.classList.add('btn-off');
                    ledOnBtn.classList.remove('btn-on');
                }
            }

            // 报警状态
            else if (topic === deviceId + '/state/beep') {
                const beepState = document.getElementById('beepState');
                const beepOnBtn = document.getElementById('beepOn');
                const beepOffBtn = document.getElementById('beepOff');
                
                if (data === '1') {
                    beepState.textContent = '报警中';
                    beepState.style.backgroundColor = 'rgba(239, 68, 68, 0.15)';
                    beepState.style.color = 'var(--danger)';
                    beepOnBtn.classList.add('btn-alert');
                    beepOnBtn.classList.remove('btn-off');
                    beepOffBtn.classList.add('btn-off');
                    beepOffBtn.classList.remove('btn-alert');
                } else {
                    beepState.textContent = '静音';
                    beepState.style.backgroundColor = 'rgba(99, 102, 241, 0.15)';
                    beepState.style.color = 'var(--primary-light)';
                    beepOffBtn.classList.add('btn-on');
                    beepOffBtn.classList.remove('btn-off');
                    beepOnBtn.classList.add('btn-alert');
                    beepOnBtn.classList.remove('btn-on');
                }
            }
        });

        // 设备控制按钮事件
        document.getElementById('ledOn').addEventListener('click', function() {
            client.publish(deviceId + '/cmd', 'a'); // 开启LED
        });

        document.getElementById('ledOff').addEventListener('click', function() {
            client.publish(deviceId + '/cmd', 'b'); // 关闭LED
        });

        document.getElementById('beepOn').addEventListener('click', function() {
            client.publish(deviceId + '/cmd', 'c'); // 启动报警
        });

        document.getElementById('beepOff').addEventListener('click', function() {
            client.publish(deviceId + '/cmd', 'd'); // 停止报警
        });
    </script>
</body>
</html>